import Dingo.API as api
import random

__author__ = "Sarah Mount"

class TheSimulation(api.Simulation):
	def __init__(self, graph):
		ether = api.Ether(self)
		sensors = (RandomBlink(graph, ether, i) for i in xrange(100))
		api.Simulation.__init__(self, sensors, graph)

	def run(self):
		super(TheSimulation, self).run()
	
class RandomBlink(api.Sensor):
	def __init__(self, graph, ether, i):
		x = random.randrange(500)
		y = random.randrange(500)
		api.Sensor.__init__(self, graph, "Sensor[%d]" % i, (x, y), api.SensorColor.Blue,
			## -1 here means run the scheduler forever
			api.Scheduler([BlinkTask(self)], -1), ether)

class BlinkTask(api.Task):
	def __init__(self, sensor):
		# self, priority, repetitions, ref-to-sensor
		api.Task.__init__(self, 0, 0, sensor)
	### This MUST be called task
	def task(self):
		node = self.sensor.node # graphical sensor
		rgb = random.randrange(1<<24)
		colour = api.SensorColor.FromRGBCombined(rgb)
		node.SetNodeColour(colour)
		sleeptime = random.uniform(0, 2)
		api.Sleep(sleeptime)
		
